# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import csv
import os.path

from itemadapter import ItemAdapter


class WawjPipeline:

    def __init__(self):
        # store_file = os.path.dirname(__file__) + '/spiders/wawj-hz.csv'
        # self.file = open(store_file, 'wb')
        # self.writer = csv.writer(self.file)
        self.f = open("wawj-hz.csv", "a", newline="")
        self.fieldnames = ["house_address", "house_condition", "house_detail_address",
                           "house_name", "house_price", "house_style", "house_way"]
        self.writer = csv.DictWriter(self.f, fieldnames=self.fieldnames)
        self.writer.writeheader()

    def process_item(self, item, spider):
        # self.writer.writerow((item["house_name"], item["house_style"], item["house_address"],
        #                       item["house_detail_address"], item["house_condition"], item["house_price"],
        #                       item["house_way"].encode('utf8')))
        # print(item)
        self.writer.writerow(item)
        return item

    def close_spider(self, spider):
        self.f.close()